
export default {

  namespace: 'todo-dva',

  state: {
    list: [
      { id: 1, name: '打王者荣耀', checked: true },
      { id: 2, name: '刷抖音', checked: false },
      { id: 3, name: '吃鸡', checked: true },
      { id: 4, name: '撩妹', checked: false },
    ],
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
    },
  },

  effects: {},

  reducers: {
    addTodo(state, { payload }) {
      state.list.push({ id: +new Date(), name: payload, checked: false })
      return { ...state }
    },
    delTodo(state, { payload }) {
      return { ...state, list: state.list.filter(item => item.id !== payload) }
    },
    editTodo(state, { payload }) {
      state.list.find(item => item.id === payload.id).name = payload.name
      return { ...state }
    },
    changeState(state, { payload }) {
      const todo = state.list.find(item => item.id === payload)
      todo.checked = !todo.checked
      return JSON.parse(JSON.stringify(state))
    },
    toggleChecked(state, { payload }) {
      state.list.forEach(item => {
        item.checked = payload
      });
      return JSON.parse(JSON.stringify(state))
    },
    clearComplated(state, { payload }) {
      return { ...state, list: state.list.filter(item => !item.checked) }
    },
  },
};
